package com.example.weather;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;
import java.util.zip.Inflater;

/**
 * Created by asd on 2016/9/20.
 */
public class ShengHuoQuan extends Activity {
    private ViewPager viewPager;
    private int[] intdata = {R.mipmap.w1, R.mipmap.w, R.mipmap.w19, R.mipmap.w18, R.mipmap.w17, R.mipmap.w16};
    private List<ImageView> ImageViewdata;
    private String[] strTitleData = {"dsadadsa", "方法都是嘎嘎发给对方", "dsadadsa", "方法都是嘎嘎发给对方", "dsadadsa", "方法都是嘎嘎发给对方"};
    private String[] strMessageData = {"我也不知道写点啥", "请各位老师见谅", "就乱BB了一些东西", "我也不知道写点啥", "请各位老师见谅", "就乱BB了一些东西"};
    private int[] intImageView1Data = {R.mipmap.w1, R.mipmap.w, R.mipmap.w19, R.mipmap.w18, R.mipmap.w17, R.mipmap.w16};
    private int[] intImageView2Data = {R.mipmap.w17, R.mipmap.w8, R.mipmap.w9, R.mipmap.w10, R.mipmap.w14, R.mipmap.w15};
    private ListView lv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shenghuoquan);
        init();
        MyListener();

    }

    //设置viewPager监听
    private void MyListener() {

    }

    private void init() {
        viewPager = (ViewPager) findViewById(R.id.viewPager);
        getData();
        MyAdapter adapter = new MyAdapter();
        viewPager.setAdapter(adapter);
        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(new MyListAdapter());
    }

    private void getData() {

        ImageViewdata = new ArrayList<>();
        for (int i = 0; i < intdata.length; i++) {
            ImageView iv = new ImageView(ShengHuoQuan.this);
            iv.setImageResource(intdata[i]);
            ImageViewdata.add(iv);
        }
    }

    //为viewPager添加适配器
    private class MyAdapter extends PagerAdapter {


        @Override
        public int getCount() {
            return ImageViewdata.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(ImageViewdata.get(position), 0);//添加页卡
            return ImageViewdata.get(position);
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(ImageViewdata.get(position));
        }
    }

    private class MyListAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return strTitleData.length;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            viewHolder holder = new viewHolder();
            if (convertView == null) {
                convertView = LayoutInflater.from(ShengHuoQuan.this).inflate(R.layout.shenghuoquan_list, null);
                holder.tv_title = (TextView) convertView.findViewById(R.id.tv_title);
                holder.tv_message = (TextView) convertView.findViewById(R.id.tv_message);
                holder.imageView1 = (ImageView) convertView.findViewById(R.id.imageView1);
                holder.imageView2 = (ImageView) convertView.findViewById(R.id.imageView2);
                holder.imageView3 = (ImageView) convertView.findViewById(R.id.imageView3);
                convertView.setTag(holder);
            } else {
                holder = (viewHolder) convertView.getTag();
            }
            holder.tv_title.setText(strTitleData[position]);
            holder.tv_message.setText(strMessageData[position]);
            holder.imageView1.setImageResource(intImageView1Data[position]);
            holder.imageView2.setImageResource(intImageView2Data[position]);
            return convertView;
        }

        private class viewHolder {
            TextView tv_title;
            TextView tv_message;
            ImageView imageView1;
            ImageView imageView2;
            ImageView imageView3;
        }
    }
}
